package com.edu.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.edu.entity.Conditions;
import com.edu.entity.Goods;
import com.edu.entity.GoodsToWeb;
import com.edu.entity.Tool;
import com.edu.entity.User;
import com.edu.service.impl.GoodServerImpl;
import com.edu.service.impl.UserServerImpl;

/**
 * Servlet implementation class goodsInfoServlet
 */
@WebServlet(name = "goodsInfo", urlPatterns = { "/goodsInfo" })
public class goodsInfoServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		Tool t1 = new Tool();
		GoodServerImpl gsi = new GoodServerImpl();
		UserServerImpl usi = new UserServerImpl();
		String getInt = request.getParameter("goodId");
		String getInt2 = request.getParameter("userId");
		int goodsId = Integer.parseInt(getInt);
		int userId = Integer.parseInt(getInt2);
		Goods g = new Goods();
		
		User u2 = gsi.findSeller(userId);
		int[] timeCount = t1.calculateDateDifference(u2.getSigndate());
		Conditions con = null;
		g.setGoodid(goodsId);
		GoodsToWeb good = gsi.indexFindGoods(g, con).get(0);
		User u1 = (User) good.getSeller();
		System.out.println("商户名；"+u1.getUsername());
		System.out.println(u1);
		good.setSeller(u2);
		int rate = (int) (u2.getFavorableRate() * 100);
		session.setAttribute("good", good);
		session.setAttribute("seller", u1);
		session.setAttribute("user", u2);
		request.setAttribute("rate", rate);
		request.setAttribute("signDate", timeCount);
		request.getRequestDispatcher("goods-info.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
